/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
<template>
  <m-list-construction title="工作流关系">
    <template slot="content">
      <div class="project-kinship-content">
        <div class="search-bar">
          <el-select
            filterable
            clearable
            :placeholder="$t('Process Name')"
            @change="onChange"
            :style="inputFocusStyle"
            v-model="currentItemName"
            v-tooltip="tooltipOption(currentItemName)"
            size="small"
          >
            <el-option
              v-for="work in workList"
              :key="work.code"
              :value="work.code"
              :label="work.name"
              v-tooltip="tooltipOption(work.name)"
            >
            </el-option>
          </el-select>
          <el-tooltip
            content="刷新"
            placement="top"
          >
            <img
              class="m-icon"
              src="./_source/img/refresh.png"
              @click="reset"
            >
          </el-tooltip>
          <el-tooltip
            content="查看"
            placement="top"
          >
            <img
              class="m-icon"
              src="./_source/img/view.png"
              @click="changeLabel"
            >
          </el-tooltip>
        </div>
        <graph-grid
          v-if="!isLoading && !!locations.length"
          :isShowLabel="isShowLabel"
        ></graph-grid>
        <template v-if="!isLoading && !locations.length">
          <m-no-data style="height: 100%;"></m-no-data>
        </template>
      </div>
      <m-spin
        :is-spin="isLoading"
        :fullscreen="false"
      ></m-spin>
    </template>
  </m-list-construction>
</template>
<script>
  import { mapActions, mapState } from 'vuex'
  import mSpin from '@/module/components/spin/spin'
  import mNoData from '@/module/components/noData/noData'
  import graphGrid from './_source/graphGrid.vue'
  import mListConstruction from '@/module/components/listConstruction/listConstruction'
  export default {
    name: 'projects-kinship-index',
    components: { graphGrid, mSpin, mNoData, mListConstruction },
    data () {
      return {
        isLoading: true,
        isShowLabel: true,
        currentItemName: ''
      }
    },
    props: {},
    methods: {
      ...mapActions('kinship', ['getWorkFlowList', 'getWorkFlowDAG', 'getWorkFlowDAGAll']),
      /**
       * init
       */
      init () {
        this.isLoading = true
        // Promise Get node needs data
        Promise.all([
          // get process definition
          this.getWorkFlowList(),
          this.getWorkFlowDAGAll()
        ]).then((data) => {
          this.isLoading = false
        }).catch(() => {
          this.isLoading = false
        })
      },
      /**
       * reset
       */
      reset () {
        this.isLoading = true
        this.$nextTick(() => {
          this.isLoading = false
        })
      },
      async onChange (item) {
        this.isLoading = true
        this.currentItemName = item
        try {
          if (item) {
            await this.getWorkFlowDAG(item)
          } else {
            await this.getWorkFlowDAGAll()
          }
        } catch (error) {
          this.$message.error(error.msg || '')
        }
        this.isLoading = false
      },
      tooltipOption (text) {
        return {
          text,
          maxWidth: '500px',
          placement: 'top',
          theme: 'dark',
          triggerEvent: 'mouseenter',
          large: false
        }
      },
      changeLabel () {
        this.isLoading = true
        this.isShowLabel = !this.isShowLabel
        this.$nextTick(() => {
          this.isLoading = false
        })
      }
    },
    watch: {
      // router
      '$route' (a) {
        // url no params get instance list
      }
    },
    created () {
      this.init()
    },
    computed: {
      ...mapState('kinship', ['locations', 'workList']),
      inputFocusStyle () {
        return 'width:280px'
      }
    },
    mounted () {
    }
  }
</script>
<style lang="scss" scoped>
.m-icon {
  width: 28px;
  height: 28px;
  margin-left: 10px;
}
</style>
<style lang="scss" rel="stylesheet/scss">
.project-kinship-content {
  position: relative;
  width: 100%;
  margin-top: 20px;
  height: calc(100vh - 180px);
  background: #ffffff url('./_source/img/dag_bg.png');
  .search-bar {
    position: absolute;
    right: 8px;
    top: 10px;
    z-index: 2;
    .ans-input {
      transition: width 300ms ease-in-out;
    }
  }
}
</style>
